package com.example.myapplication.dialogs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.adapters.AddressAdapter;
import com.example.myapplication.bean.AreaDtoResponse;
import com.example.myapplication.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class AddressDialog {

    private Context mContext;
    private Activity mActivity;
    private Dialog bottomDialog;
    private View contentView;
    private ImageView iv_close;
    private TabLayout tab_address;
    private RecyclerView rv_address;
    private AddressAdapter addressAdapter;
    private List<AreaDtoResponse.ProvinceCityResDto> mProList;
    private List<AreaDtoResponse.ProvinceCityResDto> mCityList;
    private List<AreaDtoResponse.ProvinceCityResDto> mAreaList;
    private int classCity = 1;
    private String provinceId;
    private String cityId;
    private String areaId;
    private ResultCallback mResultCallback;
    private TabLayout.Tab tab1;
    private TabLayout.Tab tab2;
    private TabLayout.Tab tab3;
    private int provincePosition = -1;
    private int cityPosition = -1;

    public AddressDialog(final Context context, final Activity activity) {
        mContext = context;
        mActivity = activity;
        bottomDialog = new Dialog(mContext, R.style.BottomDialog);
        contentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_address, null);
        initView(contentView);
        bottomDialog.setContentView(contentView);
        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
        layoutParams.width = mContext.getResources().getDisplayMetrics().widthPixels;
        layoutParams.height = mContext.getResources().getDisplayMetrics().heightPixels-400;
        contentView.setLayoutParams(layoutParams);
        bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
        bottomDialog.setCanceledOnTouchOutside(true);
    }

    /**
     * 初始化视图
     * @param contentView
     */
    private void initView(View contentView) {
        if(contentView == null || mContext == null){
            return;
        }
        iv_close = contentView.findViewById(R.id.iv_close);
        tab_address = contentView.findViewById(R.id.tab_address);
        rv_address = contentView.findViewById(R.id.rv_address);

        rv_address.setLayoutManager(new LinearLayoutManager(mContext));
        mProList = new ArrayList<>();
        addressAdapter = new AddressAdapter(mProList,mContext);
        rv_address.setAdapter(addressAdapter);
        addressAdapter.setOnItemClick(new AddressAdapter.ResultCallback() {
            @Override
            public void onSuccess(String cityName, String cityId, int position) {
                SelectAreas(cityName, cityId, position);
            }
        });
        onclickProvince(tab_address);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hide();
            }
        });
        LogUtils.i("dove","地区选择器初始化构造");
    }

    /**
     * 地区选择处理
     * @param cityName
     * @param mCityId
     */
    private void SelectAreas(String cityName, String mCityId, int position) {
        switch (classCity){
            case 1://选择省
                LogUtils.i("dove","选择省-"+cityName+"-"+mCityId);
                provinceId = mCityId;
                provincePosition = position;
                mCityList = mProList.get(position).getCitys();
                if(tab1 == null){
                    tab1 = tab_address.newTab().setText(cityName);
                    tab_address.addTab(tab1);
                }else{
                    tab1.setText(cityName);
                }
                if(tab2 == null){
                    tab2 = tab_address.newTab().setText("请选择");
                    tab_address.addTab(tab2);
                }else{
                    tab2.setText("请选择");
                }
                tab_address.setScrollPosition(1,0,true);
                if(tab3 != null){
                    tab_address.removeTab(tab3);
                    tab3 = null;
                }
                classCity = 2;
                addressAdapter.setmDataList(mCityList,-1);
                addressAdapter.notifyDataSetChanged();
                break;
            case 2://选择市
                LogUtils.i("dove","选择市-"+cityName+"-"+mCityId);
                cityId = mCityId;
                cityPosition = position;
                mAreaList = mCityList.get(position).getCitys();
//                if(tab2 == null){
//                    tab2 = tab_address.newTab().setText(cityName);
//                    tab_address.addTab(tab2);
//                }else{
                    tab2.setText(cityName);
//                }
                if(tab3 == null){
                    tab3 = tab_address.newTab().setText("请选择");
                    tab_address.addTab(tab3);
                }else{
                    tab3.setText("请选择");
                }
                tab_address.setScrollPosition(2,0,true);
                classCity = 3;
                addressAdapter.setmDataList(mAreaList,-1);
                addressAdapter.notifyDataSetChanged();
                break;
            case 3://选择区
                LogUtils.i("dove","选择区-"+cityName+"-"+mCityId);
                areaId = mCityId;
                if(tab3 == null){
                    tab3 = tab_address.newTab().setText(cityName);
                    tab_address.addTab(tab3);
                }else{
                    tab3.setText(cityName);
                }
                String cN = tab1.getText().toString().trim()
                        +tab2.getText().toString().trim()
                        +tab3.getText().toString().trim();
                mResultCallback.onSuccess(cN,provinceId,cityId,areaId);
                hide();
                break;
        }
    }

    /**
     * 切换标题监听
     * @param mtab_address
     */
    private void onclickProvince(TabLayout mtab_address) {
        if(mtab_address==null){
            return;
        }
        mtab_address.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                LogUtils.i("dove","onTabSelected-"+tab.getText()+"-"+tab.getPosition());
                notifyDataTab(tab);
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                LogUtils.i("dove","onTabUnselected-"+tab.getText()+"-"+tab.getPosition());
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                LogUtils.i("dove","onTabReselected-"+tab.getText()+"-"+tab.getPosition());
                notifyDataTab(tab);
            }
        });
    }

    /**
     * 标题切换刷新
     * @param mtab
     */
    private void notifyDataTab(TabLayout.Tab mtab) {
        if(mtab.getPosition()==2&&cityPosition!=-1){
            classCity = 3;
            addressAdapter.setmDataList(mAreaList,-1);
            LogUtils.i("dove","显示地区条数-"+mAreaList.size());
            addressAdapter.notifyDataSetChanged();
        }
        if(mtab.getPosition()==0){
            classCity = 1;
            addressAdapter.setmDataList(mProList,provincePosition);
            LogUtils.i("dove","显示地区条数-"+mProList.size());
            addressAdapter.notifyDataSetChanged();
            if(provincePosition>0){
                rv_address.scrollToPosition(provincePosition);
            }
        }
        if(mtab.getPosition()==1&&provincePosition!=-1){
            classCity = 2;
            addressAdapter.setmDataList(mCityList,cityPosition);
            LogUtils.i("dove","显示地区条数-"+mCityList.size());
            addressAdapter.notifyDataSetChanged();
            if(cityPosition>0){
                rv_address.scrollToPosition(cityPosition);
            }
        }
    }

    public void show(AreaDtoResponse madr, ResultCallback resultCallback){
        if(bottomDialog == null || addressAdapter == null || madr == null){
            return;
        }
        mResultCallback = resultCallback;
        bottomDialog.show();
        classCity = 1;
        provincePosition = -1;
        cityPosition = -1;
        mProList = madr.getData();
        if(tab1 == null){
            tab1 = tab_address.newTab().setText("请选择");
            tab_address.addTab(tab1);
        }else{
            tab1.setText("请选择");
        }
        if(tab2 != null){
            tab_address.removeTab(tab2);
            tab2 = null;
        }
        if(tab3 != null){
            tab_address.removeTab(tab3);
            tab3 = null;
        }
        addressAdapter.setmDataList(mProList,-1);
        LogUtils.i("dove","显示地区条数-"+mProList.size());
        addressAdapter.notifyDataSetChanged();
    }

    public void hide(){
        if(bottomDialog==null){
            return;
        }
        bottomDialog.dismiss();
    }

    public static abstract class ResultCallback{
        public abstract void onSuccess(String cityNames, String provinceId,
                                       String cityId, String areaId);
    }
}
